using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

namespace CentralOffice.WebClient.Controls
{
    public partial class FileUploadControl : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            for (int i = 0; i < Count; i++)
            {
                if (i > 0 && i % 3 == 0)
                {
                    Literal l = new Literal();
                    l.Text = "<br />";
                    this.Controls.Add(l);
                }
                FileUpload f = new FileUpload();
                f.ID = "FileUpload" + i.ToString();
                f.CssClass = this.CssClass;
                if (this.Width.Length > 0)
                {
                    f.Width = Unit.Parse(Width);
                }

                this.Controls.Add(f);
            }
        }

        public int Count
        {
            get { return this.ViewState["Count"] != null ? (int)this.ViewState["Count"] : 1; }
            set { this.ViewState["Count"] = value; }
        }

        public string CssClass
        {
            get { return this.ViewState["CssClass"] != null ? (string)this.ViewState["CssClass"] : "BigInput"; }
            set { this.ViewState["CssClass"] = value; }
        }

        public string Width
        {
            get { return this.ViewState["Width"] != null ? (string)this.ViewState["Width"] : "-1"; }
            set { this.ViewState["Width"] = value; }
        }

        internal string GetAttachments(string rootDir)
        {
            string returnValue = "";
            foreach (Control c in this.Controls)
            {
                FileUpload f = c as FileUpload;
                if (f != null && f.HasFile)
                {
                    returnValue += GetTempFileName(f.FileName, rootDir) + "|";
                }
            }
            return returnValue.TrimEnd('|');
        }

        internal IList<long> GetFileSizes()
        {
            List<long> sizes = new List<long>();
            foreach (Control c in this.Controls)
            {
                FileUpload f = c as FileUpload;
                if (f != null && f.HasFile)
                {
                    sizes.Add(f.FileContent.Length);
                    //returnValue += GetTempFileName(f.FileName, rootDir) + "|";
                }
            }
            return sizes;
        }
        public bool HasFiles()
        {
            foreach (Control c in this.Controls)
            {
                FileUpload f = c as FileUpload;
                if (f != null && f.HasFile)
                {
                    return true;
                }
            }
            return false;
        }
        public bool SaveFile(string rootDir)
        {
            bool success = false;
            //String savePath = SiteUtil.GetNoticesFilePath(); //Server.MapPath("~/") + "UploadFiles/";
            // rootDir = Server.MapPath(rootDir);
            try
            {
                foreach (Control c in this.Controls)
                {
                    FileUpload f = c as FileUpload;
                    if (f != null && f.HasFile)
                    {
                        f.SaveAs(Server.MapPath(GetTempFileName(f.FileName, rootDir)));
                    }
                }
                success = true;
            }
            catch
            {
                throw;
            }
            
            return success;
        }

        private static string GetTempFileName(string filename, string rootDir)
        {
            string key = filename + ApplicationContext.UserId.ToString();
            string file = HttpContext.Current.Cache[key] as String;
            if (file != null)
            {
                HttpContext.Current.Cache.Remove(key);
                return file;
            }
            file = rootDir + filename;
            string extension = System.IO.Path.GetExtension(file);
            file = System.IO.Path.GetDirectoryName(file) + System.IO.Path.DirectorySeparatorChar + System.IO.Path.GetFileNameWithoutExtension(file);
            switch (ApplicationContext.UserType)
            {
                case 2:
                    file = file + "-" + ApplicationContext.Enterprise + extension;
                    break;
                case 1:
                    file = file + "-" + ApplicationContext.Employee + extension;
                    break;
                case 0:
                    file = file + "-" + ApplicationContext.UserName + extension;
                    break;
                default:
                    break;
            }
            if (file.StartsWith("~"))
            {
                if (System.IO.File.Exists(HttpContext.Current.Server.MapPath(file)))
                {
                    file = System.IO.Path.GetDirectoryName(file) + System.IO.Path.DirectorySeparatorChar + System.IO.Path.GetFileNameWithoutExtension(file) + DateTime.Now.ToString("yyyyMMdd HHmm") + extension;
                }
            }
            else
            {
                if (System.IO.File.Exists(file))
                {
                    file = System.IO.Path.GetDirectoryName(file) + System.IO.Path.DirectorySeparatorChar + System.IO.Path.GetFileNameWithoutExtension(file) + DateTime.Now.ToString("yyyyMMdd HHmm") + extension;
                }
            }

            HttpContext.Current.Cache.Insert(key, file, null, DateTime.Now.AddSeconds(2), System.Web.Caching.Cache.NoSlidingExpiration);

            return file;
        }
    }
}